package com.kfpanda.search.index.file;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.log4j.Logger;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.pdfbox.lucene.LucenePDFDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

import com.kfpanda.search.index.AbstractFileDocument;

/**
 * ClassName: PDFDocument <br>
 * <b>Description:<b><br>
 * 将PDF转化成Document文档。
 * 
 * @Author: kfpanda
 * @Date：2012-1-6 下午10:33:28
 * @ModifyDate：2012-1-6 下午10:33:28
 */
public class PDFDocument extends AbstractFileDocument {
	
	private Logger logger = Logger.getLogger(PDFDocument.class);
	// array('Title' => '',
	// 'Author' => '',
	// 'Subject' => '',
	// 'Keywords' => '',
	// 'Creator' => '',
	// 'Producer' => '',
	// 'CreationDate' => '',
	// 'ModDate' => '',
	// );
	/**
	 * <b>Description:<b><br>
	 * 将PDF转化成Document文档。
	 * @param file
	 * @return
	 * @Author: kfpanda
	 * @ModifyUser：kfpanda
	 * @Date：2012-1-8 下午03:59:44
	 */
	@Override
	public Document document(File file) {
		Document doc = null;
		try {
			doc = LucenePDFDocument.getDocument(file);
		} catch (IOException e) {
			logger.error("转化PDF文件（" + file.getPath() + "）到Document时出错。", e);
		}
		String stemp = this.fileReaderAll(file.getPath(), "UTF-8", 10);
		doc.add(new Field("intro", stemp, Field.Store.YES, Field.Index.ANALYZED));
		
		doc.add(new Field("filePath", file.getPath(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("fileName", file.getName(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("lastModified", DateTools.timeToString(
				file.lastModified(), DateTools.Resolution.MINUTE),
							Field.Store.YES, Field.Index.NOT_ANALYZED));
		
		return doc;
	}

	/**
	 * <b>Description:<b><br>
	 * 获得指定行数的文本文件内容。
	 * @param filePath
	 * @param charset
	 * @param rowNum
	 * @return
	 * @Author: kfpanda
	 * @ModifyUser：kfpanda
	 * @Date：2012-1-8 下午03:59:44
	 */
	@Override
	public String fileReaderAll(String filePath, String charset, int rowNum) {
		/*
		 * COSDocument cosDoc = null; PDFParser parser = new
		 * PDFParser(inputStream); parser.parse(); cosDoc =
		 * parser.getDocument(); if (cosDoc.isEncrypted()) { DecryptDocument
		 * decryptor = new DecryptDocument(cosDoc);
		 * decryptor.decryptDocument(password); } PDFTextStripper stripper = new
		 * PDFTextStripper(); String docText = stripper.getText(new
		 * PDDocument(cosDoc));
		 */
		PDFTextStripper aStripper = null;
		String result = "";
		PDDocument pdfDoc = null; 
		try {
			aStripper = new PDFTextStripper();
			aStripper.setStartPage(1);
			aStripper.setEndPage(rowNum);
			pdfDoc = PDDocument.load(filePath);	
			result = aStripper.getText(pdfDoc);
			pdfDoc.close();
		} catch (IOException e) {
			logger.error("pdf 获取摘要内容出错。", e);
		}finally{
			if(pdfDoc != null){		
				try {
					pdfDoc.close();
				} catch (IOException e) {
					logger.error("pdf 获取摘要内容出错。", e);
				}		
			}
		}
		return result;
	}
	
	
	public static void main(String[] args) throws MalformedURLException, IOException {
		File f = new File("C:\\Users\\shapecool\\Desktop\\需求规范\\Lucene in Action(1).pdf");
		/*//		PDFDocument pdfDoc = new PDFDocument();
//		Document doc = pdfDoc.document(f);
		WordDocument wdDoc = new WordDocument();
		Document doc = wdDoc.document(f);
		System.out.println(doc.get("filePath"));
		String fileUrl = "file://C:/Users/shapecool/Desktop/需求规范/sdfsdf.doc";
		System.out.println(PDFDocument.getMimeType(fileUrl));
		*/
		PDFDocument doc = new PDFDocument();
		Document dt = doc.document(f);
		System.out.println(dt.get("intro"));
//        System.out.println(MimeUtils.getMimeType(f));

	}
}
